//
//  MerchantsListViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/18.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MerchantsListViewController.h"
#import "MerchantViewController.h"

#import "KK_TabBarView.h"
#import "MerchantsListTableViewCell.h"

#import "MerchantModel.h"

@interface MerchantsListViewController ()<UITableViewDataSource,UITableViewDelegate,KK_TabBarViewDelegate>
@property (nonatomic , weak) IBOutlet UIView *topView;
@property (nonatomic , weak) IBOutlet UITableView *tableview;

@property (nonatomic , strong) KK_TabBarView *tabBarView;
@property (nonatomic , strong) NSMutableArray *tabBarItems;//分类选项
@property (nonatomic , strong) NSMutableArray *tabBarLabels;//分类选项
@property (nonatomic , assign) NSInteger typeIndex;

@property (nonatomic , strong) NSMutableArray *merchantsList;// 商家列表
@end

@implementation MerchantsListViewController

-(void)awakeFromNib
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _tabBarItems = [NSMutableArray array];
    _tabBarLabels = [NSMutableArray array];
    _merchantsList = [NSMutableArray array];
    [self updateMerchantTypes];
//    [self fetchMerchantsListWithColId:nil key:nil typeId:nil currentPage:1 pageSize:200];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 刷新商家分类
-(void)updateMerchantTypes
{
    typeof(self) self_ = self;
    [[MerchantModel sharedModel]fetchMerchantTypeList:^(NSArray *typeList, NSString *msg) {
        [self_.tabBarItems removeAllObjects];
        [self_.tabBarLabels removeAllObjects];
        
//        MerchantTypeItem *allTypeItem = [MerchantTypeItem new];
//        [self_.tabBarItems addObject:allTypeItem];
//        [self_.tabBarLabels addObject:@"全部"];
        
        for (MerchantTypeItem *item in typeList) {
            [self_.tabBarItems addObject:item];
            [self_.tabBarLabels addObject:item.TYPE_NAME];
        }
        
        [self_ insertTabBarView:self_.tabBarLabels];
    }];
}

-(void)insertTabBarView:(NSArray *)souresList
{
    if (!_tabBarView) {
        _tabBarView = [[KK_TabBarView alloc]initWithDataSource:souresList frame:_topView.bounds selectedColor:[UIColor redColor]];
        _tabBarView.delegate = self;
        [_topView addSubview:_tabBarView];
    }
    [_tabBarView relodData:souresList];
    _typeIndex = 0;
    [self tabBarView:self.tabBarView didSelectIndex:0];
}

#pragma mark - 获取商家列表
-(void)fetchMerchantsListWithColId:(NSString *)colId
                               key:(NSString *)key
                            typeId:(NSString *)typeId
                       currentPage:(int)currentPage
                          pageSize:(int)pageSize
{
    typeof(self) self_ = self;
    [self.merchantsList removeAllObjects];
    [[MerchantModel sharedModel]fetchMerchantsListWithColId:colId key:key typeId:typeId currentPage:currentPage pageSize:pageSize result:^(NSArray *list, NSString *msg) {
        [self_.merchantsList addObjectsFromArray:list];
        [self_.tableview reloadData];
    }];
}

#pragma mark - KK_TabBarView delegate
- (void)tabBarView:(KK_TabBarView *)tabBarView didSelectIndex:(NSInteger)index
{

    
    MerchantTypeItem *item = _tabBarItems[index];

    [self fetchMerchantsListWithColId:nil key:nil typeId:item.TYPE_ID currentPage:1 pageSize:200];
}


#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_merchantsList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MerchantsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantsListTableViewCell"];
    
    MerchantItem *item = _merchantsList[indexPath.row];
    [cell updateDataWithItem:item];
    
    return cell;
    
}




#pragma mark - table delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([sender isKindOfClass:[MerchantsListTableViewCell class]]) {
        NSIndexPath *indexPath = [_tableview indexPathForCell:sender];

        MerchantItem *item = _merchantsList[indexPath.row];
        
        MerchantViewController *merchantViewController = [segue destinationViewController];
        merchantViewController.merchantid = item.MERCHANT_ID;
    }
}

@end
